package com.controller;

import com.pojo.User;
import com.services.UserService;
import com.utils.Result;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据
        Map<String, String[]> map = req.getParameterMap();
        //封装数据
        User u = new User();
        try {
            BeanUtils.populate(u, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取生成的验证码
        String code = (String) req.getSession().getAttribute("RANDOMCODE_IN_SESSION");
        //获取用户输入的验证码
        String checkCode = req.getParameter("checkCode");
        //调用业务层方法
        UserService service = new UserService();
        Result re = service.login(u);
        if (code.equals(checkCode)) {
            if (re.isSuccess()){
                //将用户名信息存放到session
                req.getSession().setAttribute("LOGIN_NAME", u.getUsername());
                //重定向
                resp.sendRedirect(req.getContextPath() + "/emp/page");
            }else {
                //存放错误信息
                req.getSession().setAttribute("msg",re.getMsg());
                resp.sendRedirect(req.getContextPath() + "/login.jsp");
            }
        }else {
            req.getSession().setAttribute("msg", "验证码错误");
            resp.sendRedirect(req.getContextPath() + "/login.jsp");
        }
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单信息
        Map<String, String[]> map = req.getParameterMap();
        //封装数据
        User u = new User();
        try {
            BeanUtils.populate(u, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service层存放数据
        UserService service = new UserService();
        //判断用户名是否重复
        Result re = service.getUsername(u.getUsername());
        if (re.isSuccess()){
            req.getSession().setAttribute("REGISTER", re.getMsg());
            resp.sendRedirect(req.getContextPath() + "/regist.jsp");
        }else {
            //不存在用户
            //向数据库中存放数据
            Result result = service.execute(u);
            req.getSession().setAttribute("REGISTER", result.getMsg());
            resp.sendRedirect(req.getContextPath() + "/regist.jsp");
        }
    }

    public void changePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取提交信息
        Map<String, String[]> map = req.getParameterMap();
        //封装数据
        User u = new User();
        try {
            BeanUtils.populate(u, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service层比较用户输入的用户名和数据库中的用户名是否一一致
        UserService service = new UserService();
        Result result = service.setPwd(u);
        req.getSession().setAttribute("REMOVE_PWD", result.getMsg());
        resp.sendRedirect(req.getContextPath() + "/findPwd.jsp");
    }

    public void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //清除session
        req.getSession().removeAttribute("LOGIN_NAME");
        //重定向
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    }
}
